<?php 
class GetIPDetails{		

	// Function for validate IP Address
	function validIp($ip) {
	 
			if (!empty($ip) && ip2long($ip)!=-1) {
			 
				$reserved_ips = array (
				array('0.0.0.0','2.255.255.255'), 
				array('10.0.0.0','10.255.255.255'),	 
				array('127.0.0.0','127.255.255.255'),
				array('169.254.0.0','169.254.255.255'),
				array('172.16.0.0','172.31.255.255'),
				array('192.0.2.0','192.0.2.255'),
				array('192.168.0.0','192.168.255.255'),
				array('255.255.255.0','255.255.255.255')				 
				);
				 
				foreach ($reserved_ips as $r) {
					$min = ip2long($r[0]);
					$max = ip2long($r[1]);
				 
					if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;	 
				}
				 
				return true;
			}else{
				return false;
			}
	 }
 	// Function to find Current IP
	function getIp() {
	 
			if ($this->validIp(@$_SERVER["HTTP_CLIENT_IP"])) {			 
				return $_SERVER["HTTP_CLIENT_IP"];			 
			}
			 
			foreach (explode(",",@$_SERVER["HTTP_X_FORWARDED_FOR"]) as $ip) {
			 
				if ($this->validIp(trim($ip))) {				 
					return $ip;				 
				}
			 
			}
			 
			if ($this->validIp(@$_SERVER["HTTP_X_FORWARDED"])) {
			 
				return $_SERVER["HTTP_X_FORWARDED"];
			 
			} elseif ($this->validIp(@$_SERVER["HTTP_FORWARDED_FOR"])) {
			 
				return $_SERVER["HTTP_FORWARDED_FOR"];
			 
			} elseif ($this->validIp(@$_SERVER["HTTP_FORWARDED"])) {
			 
				return $_SERVER["HTTP_FORWARDED"];
			 
			} elseif ($this->validIp(@$_SERVER["HTTP_X_FORWARDED"])) {
			 
				return $_SERVER["HTTP_X_FORWARDED"];
			 
			} else {
			 
				return @$_SERVER["REMOTE_ADDR"];
			 
			}
	 }	  	
	
	function get_ip_details($ip_address='')
	{
		if(!$ip_address) $ip_address= $this->getIp();
		
		//get the JSON result from hostip.info
		$result = file_get_contents("http://api.hostip.info/get_json.php?ip=".$ip_address);
	
		$result = json_decode($result, 1);
	
		//return the array containing city, country and country code
		return $result;
	}
	
	//Function to check Internet Connection
	function is_connected()
	{
		$connected = @fsockopen("www.google.com", 80,$num,$error,5);
		if ($connected){
			$is_conn = true; //action when connected
			fclose($connected);
		}else{
			$is_conn = false; //action in connection failure
		}
		return $is_conn;
	
	}
	

}
